import { post, postFile, get, put } from '@/utils/request';

export default {
    namespaced: true,
    state: {
        // 仓库数据做持久化的操作
        isLogin: '',
        userMsg: {},
        project_id: 241
    },
    mutations: {
        changeIsLogin(state, payload) {
            state.isLogin = payload.token;
            state.userMsg = payload.userMsg;
            sessionStorage.setItem('isLogin', state.isLogin);
        }
    },
    actions: {
        addLogin({ commit }, userMsg) {
            return new Promise((resolve, reject) => {
                post('/login', {
                        phone: userMsg.userPhone,
                        password: userMsg.userPassword
                    })
                    .then(res => {
                        sessionStorage.setItem('my', JSON.stringify(res.result));
                        // sessionStorage.setItem('userName', res.result.name);
                        commit('changeIsLogin', { token: res.result.token, userMsg: res.result });
                        resolve(res.result);
                    })
                    .catch(err => {
                        reject(err);
                    });
            });
        },

        registerUser(ctx, registerMsg) {
            return new Promise((resolve, reject) => {
                post('/register', {
                        name: registerMsg.userName,
                        password: registerMsg.userPassword,
                        phone: registerMsg.userPhone
                    })
                    .then(res => {
                        resolve(res.result);
                    })
                    .catch(err => {
                        reject(err);
                    });
            });
        }

        // 异步操作 Vuex的上下文指的是Vuex里面的所有东西 ，包括commit
    }
};